package cn.simple.manager.web;

import cn.simple.manager.Repository.BannerResponsitory;
import cn.simple.manager.Repository.FontStorageResponsitory;
import cn.simple.manager.Repository.ImageStorageResponsitory;
import cn.simple.manager.todo.BannerStorage;
import cn.simple.manager.todo.FontStorage;
import cn.simple.manager.todo.ImageStorage;
import cn.simple.manager.todo.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class SysApiController {
    @Autowired
    private ImageStorageResponsitory imageStorageResponsitory;

    @Autowired
    private BannerResponsitory bannerResponsitory;

    @Autowired
    FontStorageResponsitory fontStorageResponsitory;

    /**
     *
     * @param imageStorages  上传图片详情实体 只需要传sort TagName
     * @return
     */
    @PostMapping("/add/img")
    public ResponseEntity addImage(@RequestBody List<ImageStorage> imageStorages){
        if(null!=imageStorages && imageStorages.size()>0){
           List<ImageStorage> storages = imageStorages.stream().map(imageStorage -> {
                imageStorage.setId(UUID.randomUUID().toString());
                imageStorage.setCreateTime(new Date());
                return imageStorage;
            }).collect(Collectors.toList());
          List<ImageStorage> storage=imageStorageResponsitory.saveAll(storages);;
          return ResponseEntity.ok(storage);
        }
        return ResponseEntity.ok(null);
    }

    /**
     *
     * @param id 图片id
     * @return
     */
    @RequestMapping("/img/delete")
    public  ResponseEntity deleteImage(String id){
        imageStorageResponsitory.deleteById(id);
        return  ResponseEntity.ok(null);
    }

    /**
     * banner页的新增
     * @param bannerStorages   mediaid 不可传 自动过滤 type不可包含wechat的
     * @return
     */
    @RequestMapping("/banner/add")
    public ResponseEntity bannerAdd(@RequestBody List<BannerStorage> bannerStorages){
        if(bannerStorages.size()>0){
            List<BannerStorage> storages = bannerStorages.stream().
                    filter(banner->banner.getMediaId()==null&&banner.getType().toLowerCase().indexOf("Wechat")==-1)
                    .map(banner->{
                        banner.setCreateTime(new Date());
                        banner.setUpdateTime(new Date());
                        return  banner;
                    })
                    .collect(Collectors.toList());
            List<BannerStorage> re = bannerResponsitory.saveAll(storages);
            return ResponseEntity.ok(re);
        }

        return ResponseEntity.ok(null);
    }

    @RequestMapping("/font/add")
    public ResponseEntity addFontStorages(List<FontStorage> fontStorages){
        if(fontStorages.size()>0){
            List<FontStorage> storages = fontStorages.stream().map(font->{
                font.setCreateTime(new Date());
                return font;
            }).collect(Collectors.toList());
            List<FontStorage> re = fontStorageResponsitory.saveAll(storages);
            return ResponseEntity.ok(re);
        }
        return ResponseEntity.ok(null);
    }
}
